/**
 * Created by wd on 2016/11/19.
 */
var loading = false;
var page = 1;
var last = false;
function addItems() {
    /*获取数据*/
    api.ajax({
        url : getAPIDomain() + 'apiyerList/0/0/' + page,
        method : 'get',
    }, function(ret, err) {
        /*数据处理*/
        if (ret && ret.data) {
            // 生成新条目的HTML
            var html = '';
            var data = ret.data;
            for (var i = 0; i < ret.data.length; i++) {
                if (!data[i].nickname) {
                    data[i].nickname = data[i].username;
                }
                var stars = data[i].comment_total / data[i].comment_number;
                var starHtml = '';
                for (var j = 1; j <= 5; ++j) {
                    if (stars - j >= 0) {
                        starHtml += '<span class="icon icon-onstar"></span>';
                    } else {
                        starHtml += '<span class="icon icon-onstar" style="color:#b0b0b0;"></span>';
                    }
                }
                var order = '';
                if (data[i].order_number > 0) {
                    order = '订单:&nbsp;' + data[i].order_number;
                }
                html += '<li onclick="buyer(\'' + data[i].nickname + '\',' + data[i].id + ',' + data[i].uid + ',\'' + data[i].telephone + '\',\'' + getQiniuDomain() + data[i].avatar + '\')">' + '<div class="card demo-card-header-pic">' + '<div valign="bottom" class="card-header color-white no-border no-padding">' + '<img class="card-cover" src="' + getQiniuDomain() + data[i].background + '?imageMogr2/thumbnail/' + api.screenWidth + 'x/gravity/Center/crop/x' + Math.ceil(api.screenWidth / 2) + '/quality/80" alt="">' + '<div class="service-designer-info">' + '<div class="pull-left">' + '<img class="service-list-avatar" src="' + getQiniuDomain() + data[i].avatar + '">' + '</div>' + '<div class="service-list-text pull-left">' + '<div>' + data[i].nickname + '</div>' + '<div>' + starHtml + '</div>' + '</div>' + '<div class="clearfix"></div>' + '</div>' + '</div>' + '<div class="card-footer">' + '<a>' + order + '</a>' + ' ' + data[i].category_name + ' - ' + data[i].brand_name + '</div>' + '</div>' + '<>';
            }
            // 添加新条目
            $('.infinite-scroll-bottom .list-container').append(html);
            loading = false;
            page++;
        } else {
            alert(JSON.stringify(err));
        }
        if (ret.last_page < page) {
            last = true;
        }
        if (ret.total == 0 || err || last) {
            loading = false;
            last = true;
            // 加载完毕，则注销无限加载事件，以防不必要的加载
            $.detachInfiniteScroll($('.infinite-scroll'));
            // 删除加载提示符
            $('.infinite-scroll-preloader').remove();
            return;
        }
    });
}

